using System;
using System.Collections.Generic;
using System.Text;

namespace RapidHDL
{
    public class Constant : Component 
    {
        public NodeVector OutputNodes;
        int iValue;
        string sValue;

        public Constant(Component poParentComponent, string psName, int piWidth, int piValue)
            : base(poParentComponent, psName)
        {
            OutputNodes = this.CreateNodeVector("DATA_O", piWidth,NodeFlowType.Source);
            iValue = piValue;
            sValue = "";
        }

        public Constant(Component poParentComponent, string psName, int piValue)
            : base(poParentComponent, psName)
        {
            sValue = Conversion.IntToBinaryString(piValue);
            OutputNodes = this.CreateNodeVector("DATA_O", sValue.Length, NodeFlowType.Source);
            iValue = 0;
        }

        public Constant(Component poParentComponent, string psName, string psValue)
            : base(poParentComponent, psName)
        {
            OutputNodes = this.CreateNodeVector("DATA_O", psValue.Length, NodeFlowType.Source);
            iValue = 0;
            sValue = psValue;
        }

        public override void GenerateStructure()
        {
            if (sValue == "")
                OutputNodes.NodeVectorAsString = Conversion.IntToBinaryString(iValue, OutputNodes.Nodes.Count);
            else
                OutputNodes.NodeVectorAsString = sValue;
        }

        public override bool InitializeSimulation()
        {
            this.SimulationDestabilizeComponent();
            this.SimulationStabilizeComponent();
            return true;
        }

        public override bool TransformStructureToVerilog()
        {
            if (sValue == "")
                ComponentVerilog.WriteVerilogText("assign " + OutputNodes.Name + " = " + OutputNodes.Nodes.Count.ToString() + "'b" + Conversion.IntToBinaryString(iValue, OutputNodes.Nodes.Count) + ";",2);
            else
                ComponentVerilog.WriteVerilogText("assign " + OutputNodes.Name + " = " + OutputNodes.Nodes.Count.ToString() + "'b" + sValue + ";", 2);
            return true;
        }
    }
}
